import datetime

from django.test import TestCase
from django.db import models, connection
import django.contrib.contenttypes.models as ct
from django.contrib.contenttypes.management import update_all_contenttypes

import slots.models as slots_models


# two simple models we're going to create relationships between in the slot and
# transition tests
class Header(models.Model):
    name = models.CharField(max_length=128)
    class Meta:
        db_table = 'slots_header'

class Page(slots_models.ScheduleMixin, models.Model):
    title = models.CharField(max_length=128)
    schedules = (Header,)
    class Meta:
        db_table = 'slots_page'

class Sponsor(models.Model):
    sponsor = models.CharField(max_length=128)
    class Meta:
        db_table = 'slots_sponsor'


# the SQL for the above models' tables
def create_models():
    cursor = connection.cursor()
    cursor.execute("""CREATE TABLE `slots_page` (
                        `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
                        `title` varchar(128)
                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;""")
    cursor.execute("""CREATE TABLE `slots_header` (
                        `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
                        `name` varchar(128)
                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;""")
    cursor.execute("""CREATE TABLE `slots_sponsor` (
                        `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
                        `sponsor` varchar(128)
                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;""")

# drop the tables when we're done
def destroy_models():
    cursor = connection.cursor()
    cursor.execute("""DROP TABLE `slots_page`;""")
    cursor.execute("""DROP TABLE `slots_header`;""")
    cursor.execute("""DROP TABLE `slots_sponsor`;""")

class TimingTest(TestCase):
    one_second = datetime.timedelta(seconds=1)
    two_seconds = datetime.timedelta(seconds=2)
    three_seconds = datetime.timedelta(seconds=3)
    five_seconds = datetime.timedelta(seconds=5)
    thirty_seconds = datetime.timedelta(seconds=30)
    @property
    def five_seconds_time(self):
        return datetime.datetime.now() + self.five_seconds
    def setUp(self):
        # create the models and instances
        create_models()
        # we need content types for them
        update_all_contenttypes()
        self.page = Page(title="page one")
        self.page.save()
        self.header1 = Header(name="header one")
        self.header1.save()
        self.header2 = Header(name="header two")
        self.header2.save()
        self.sponsor = Sponsor(sponsor="Harmonix")
        self.sponsor.save()
        self.page_ct = ct.ContentType.objects.get_for_model(Page)
        self.header_ct = ct.ContentType.objects.get_for_model(Header)
    def tearDown(self):
        destroy_models()
